package com.fendaren.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 构建简单模式的生产者，发送消息
 */
public class FooProducer {

    public static void main(String[] args) throws IOException, TimeoutException {

        // 1.创建连接以及相关的参数配置
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.99.178");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("fendaren");
        factory.setPassword("fendaren");

        // 2.通过工厂创建连接Connection
        Connection connection = factory.newConnection();

        // 3.创建管道Channle
        Channel channel = connection.createChannel();

        // 4.创建队列Queue
        channel.queueDeclare("hello",true,false,false,null);

        // 5.向队列发送消息
        String msg = "Hello World!";
        channel.basicPublish("","hello",null,msg.getBytes());

        // 6.释放资源
        channel.close();
        connection.close();
    }
}
